<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/plugins/select2/select2.css" />
    <link rel="stylesheet" href="/plugins/select2/select2-bootstrap.css" />
    <script src="/plugins/select2/select2.min.js" type="text/javascript"></script>

    <script>
        //页面加载事件
        $(function(){
            show();
            $("#brand").select2();//增加
            $("#spec").select2();
            $("#updateBrand").select2();//修改
            $("#updateSpec").select2();
        })

        //select2的取值方式
        //我们以前下拉框的取值方式：$("#id").val()

 /*       function save(){
            var brandData =  $("#brand").select2("data");
            console.log(brandData);
            //定义js数组,js中不分数组和集合，java中区分，并且java中数组一旦定义不能改变其大小，js可以随意改变
            var brandList = [];
            //闭包,java不支持闭包 代替js闭包 匿名内部类
            $.each(brandData,function(i,e){
                var brand = {id:e.id,text:e.text};
                brandList.push(brand);
            })
            //将数组对象转成json格式的字符串
            var brandlistJson =  JSON.stringify(brandList);

            var customAttributeList = [];
            //组装扩展属性数据
            var trList =	$("#customAttr").find("tr"); //获取table表格所有的tr
            for(var i=1; i<trList.length; i++){ //从第二行开始遍历 ， 第一行是表头
                var tds = $(trList[i]).find("td"); // 获取当前行的所有的td列
                var value = $(tds[1]).find("input").val(); //取第二列的input中的值
                var customAttribute = {text:value};  //将取到的值封装成对象
                customAttributeList.push(customAttribute); //将对象存入list集合中
            }
            var customAttributeListJson =  JSON.stringify(customAttributeList);//将集合转成json对象
            // JSON.parse() 将字符串转成json对象
            console.log(customAttributeListJson);
        }*/

        //删除表格
        function deleteId(obj) {
            //获取表
            var parent=obj.parentNode.parentNode;
            parent.parentNode.removeChild(parent);
        }

        //增加操作
        function insertTable() {
            //获取table的id
            var  tbody=document.getElementById("templateTable");
            //创建tr标签
            var tr=document.createElement("tr");
            //设置tr样式
            tr.innerHTML = '<td><input type="checkbox" class="icheckbox_square-blue" ></td>' + '<td><input class="form-control" placeholder="属性名称" ></td>'+
                '<td><button type="button" class="btn btn-default" title="删除" onclick="deleteId(this)"><i class="fa fa-trash-o"></i> 删除</button> </td>';
            //添加tr标签
            tbody.appendChild(tr);
        }
        //修改窗口的表格增加操作
        function updateTable(){
            //获取table的id
            var tbody = document.getElementById("updateTable");
            //创建tr标签
            var tr = document.createElement("tr");
            //设置tr的样式
            tr.innerHTML = '<td><input type="checkbox" class="icheckbox_square-blue" ></td>' +
                '<td><input class="form-control" placeholder="属性名称" ></td>'+
                '<td> <button type="button" class="btn btn-default" title="删除" onclick="deleteId(this)"><i class="fa fa-trash-o"></i> 删除</button> </td>';
            //添加tr标签
            tbody.appendChild(tr);
        }


        //查询
        function show() {
            //alert("进入页面")
            var name=$("#getName").val();
            $.ajax({
                url:"/template/listTemplate",
                dataType:"json",
                type:"post",
                data:{name:name},
                success:function (result) {
                    if (result.code == 200){
                    var data = result.data;
                    var tbody="";
                    $(data).each(function (i,e) {
                        var specIdsList = JSON.parse(e.specIds);//转成json对象关联规格
                        var brandIdsList = JSON.parse(e.brandIds);//关联品牌
                        var customAttributeItemsList = JSON.parse(e.customAttributeItems);//自定义属性
                        tbody += '<tr>';
                        tbody += '<td><input  type="checkbox" name="box" value="' + data[i].id + '"></td>';
                        tbody += '<td>' + data[i].id + '</td>'
                        tbody += '<td>' + data[i].name + '</td>';
                        tbody += '<td>';
                        $.each(brandIdsList,function (i1,e1) {
                            if (i1 == brandIdsList.length -1){
                                tbody += e1.text;
                            }else {
                                tbody +=e1.text +',';
                            }
                        });
                        tbody += '</td>';
                        tbody += '<td>';
                        $.each(specIdsList,function (i2,e2) {
                            if (i2 ==brandIdsList.length -1){
                                tbody += e2.text;
                            }else {
                                tbody +=e2.text +',';
                            }
                        });
                        tbody += '</td>';
                        tbody += '<td>';
                        $.each(customAttributeItemsList,function (i3,e3) {
                            if (i3 ==customAttributeItemsList.length -1){
                                tbody += e3.text;
                            }else {
                                tbody +=e3.text +',';
                            }
                        });
                        tbody += '</td>';
                        tbody += '<td class="text-center">';
                        tbody += '<button type="button" class="btn bg-olive btn-xs" onclick="toUpdate(' + data[i].id + ')" data-toggle="modal" data-target="#updateModal">修改</button>';
                        tbody += '</td>';
                        tbody += '</tr>';
                    })
                    }
                    $("#tbody").html(tbody);
                },
                error:function (result) {
                    alert(result.message);
                }
            })
        }
        //删除
        function deleteTemplate() {
            var boxs=$("[type=checkbox]:checked");
            var ids=[];
            $(boxs).each(function (i,e) {
                ids.push(e.value);
            })
            if (ids.length<=0){
                return;
            }
            $.ajax({
                url:"/template/deleteIds",
                type:"post",
                dataType:"json",
                data:{ids:ids},
                success:function (result) {
                    alert(result.message);
                    location.reload();
                },
                error:function (result) {
                    alert(result.message);
                }
            })
        }

        //增加
        function insert() {
            alert(1111)
            //商品类型
            var name=$("#name").val();
            //关联品牌
            var brandData=$("#brand").select2("data");
            //定义数组
            var brandList=[];
            $.each(brandData,function (index,e) {
                var brand={id:e.id,text:e.text};
                brandList.push(brand);
            })
            //转成字符串
            var brandJson = JSON.stringify(brandList);
            //关联规格
            var specData = $("#spec").select2("data");
            var specList=[];
            $.each(specData,function (i,e) {
                var spec={id:e.id,text:e.text};
                specList.push(spec)
            })
            let specJson = JSON.stringify(specList);
            //扩展属性
            var customList=[];
            $("#templateTable").find("tr").each(function () {
                var tdArr = $(this).children();
                var custom=tdArr.eq(1).find("input").val();//属性名称
                var customs={"text":custom};
                customList.push(customs);
            })
            var customJson = JSON.stringify(customList);
            $.ajax({
                url:"/template/addTemplate",
                type:"post",
                dataType:"json",
                data:{name:name,brandIds:brandJson,specIds:specJson,customAttributeItems:customJson},
                success:function (result) {
                    alert(result.message);
                    location.reload();
                },
                error:function (result) {
                    alert("失败1");
                }
            })
        }
        //回显
        function toUpdate(id) {
            $.ajax({
                url:"/template/updateById/"+id,
                type:"post",
                dataType:"json",
                success:function (result) {
                    $("#temId").val(result.data.id);
                    $("#tempName").val(result.data.name);
                    var brandJson=JSON.parse(result.data.brandIds);//关联品牌 转成String
                    var specJson=JSON.parse(result.data.specIds);//关联规格
                    var customJson=JSON.parse(result.data.customAttributeItems);//扩展属性
                    var brands=[];
                    var specs=[];
                    $.each(brandJson,function (i,e) {
                        brands.push(e.id);
                    })
                    $.each(specJson,function (i,e) {
                        specs.push(e.id);
                    })
                    //select2()回显
                    $("#updateBrand").val(brands).trigger("change");
                    $("#updateSpec").val(specs).trigger("change");
                    var tbody="";
                    for (var  i=0;i<customJson.length;i++){
                        tbody +='<tr>';
                        tbody += '<td><input type="checkbox" class="icheckbox_square-blue" ></td>'
                        tbody += '<td><input class="form-control" placeholder="属性名称" value="'+customJson[i].text+'"></td>';
                        tbody += '<td><button type="button" class="btn btn-default" title="删除" onclick="deleteId(this)"><i class="fa fa-trash-o"></i> 删除</button></td>';
                        tbody += '</tr>';
                    }
                    $("#updateTable").html(tbody);
                },
                error:function (result) {
                    alert("失败");
                }
            })
        }
        //修改
        function updateTemplate() {
            var id=$("#temId").val();
            var name=$("#tempName").val();
            //关联品牌
            var brandData=$("#updateBrand").select2("data");
            //定义数组
            var brandList=[];
            $.each(brandData,function (index,e) {
                var brand={id:e.id,text:e.text};
                brandList.push(brand);
            })
            //转成字符串
            var brandJson = JSON.stringify(brandList);
            //关联规格
            var specData = $("#updateSpec").select2("data");
            var specList=[];
            $.each(specData,function (i,e) {
                var spec={id:e.id,text:e.text};
                specList.push(spec)
            })
            var specJson = JSON.stringify(specList);
            //扩展属性
            var customList=[];
            $("#updateTable").find("tr").each(function () {
                var tdArr = $(this).children();
                var custom=tdArr.eq(1).find("input").val();//属性名称
                var customs={"text":custom};
                customList.push(customs);
            })
            var customJson = JSON.stringify(customList);
            $.ajax({
                url:"/template/updateTemplate",
                type:"post",
                dataType:"json",
                data:{id:id,name:name,brandIds:brandJson,specIds:specJson,customAttributeItems:customJson},
                success:function (result) {
                    alert(result.message);
                    location.reload();
                },
                error:function (result) {
                    alert(result.message);
                }
            })
        }


    </script>
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" onclick="deleteTemplate()" title="删除"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input id="getName" >
									<button class="btn btn-default" onclick="show()">查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tbody">

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->
                        </div>
                        <!-- 数据表格 /-->

                     </div>
                    <!-- /.box-body -->
                    
	           
					    
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input id="name"  class="form-control" placeholder="商品类型">  </td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>
		      			<select id="brand" name="brand" multiple style="width: 100%">
                            <option th:each="brand : ${brandAll}" th:value="${brand.id}">[[${brand.name}]]</option>
                        </select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td>
	                    <select id="spec" name="spec" multiple style="width: 100%">
                            <option th:each="spec : ${specAll}" th:value="${spec.id}">[[${spec.specName}]]</option>
                        </select>
		      		</td>
		      	</tr>	
		      	
		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" class="btn btn-default" title="新增扩展属性"  onclick="insertTable()"><i class="fa fa-file-o"></i> 新增扩展属性</button>
                                                                    
                        </div>
						<table  class="table table-bordered table-striped"  width="800px">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>									
									<td>操作</td>
								</tr>
							</thead>
							<tbody id="templateTable">
                            <tr>
                                <td><input type="checkbox" class="icheckbox_square-blue" ></td>
                                <td><input class="form-control" placeholder="属性名称" ></td>
                                <td><button type="button" class="btn btn-default" title="删除" onclick="deleteId(this)"><i class="fa fa-trash-o"></i> 删除</button></td>
                            </tr>
                            <tr>
                                <td><input type="checkbox" class="icheckbox_square-blue" ></td>
                                <td><input class="form-control" placeholder="属性名称" ></td>
                                <td><button type="button" class="btn btn-default" title="删除" onclick="deleteId(this)"><i class="fa fa-trash-o"></i> 删除</button></td>
                            </tr>
							</tbody>
						</table>	
           
		      		</td>
		      	</tr>	
		      	      
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="insert()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 修改窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog" >
          <div class="modal-content">
              <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                  <h3 id="myModalLabel1">商品类型模板编辑</h3>
              </div>
              <div class="modal-body">

                  <table class="table table-bordered table-striped"  width="800px">
                      <tr>
                          <td>
                              <input type="hidden" name="id" id="temId">
                          </td>
                      </tr>
                      <tr>
                          <td>商品类型</td>
                          <td><input  class="form-control" placeholder="商品类型" id="tempName" name="name">  </td>
                      </tr>
                      <tr>
                          <td>关联品牌</td>
                          <td>
                              <select id="updateBrand" name="brand" multiple style="width: 100%">
                                  <option th:each="brand : ${brandAll}" th:value="${brand.id}">[[${brand.name}]]</option>
                              </select>
                          </td>
                      </tr>
                      <tr>
                          <td>关联规格</td>
                          <td>
                              <select id="updateSpec" name="spec" multiple style="width: 100%">
                                  <option th:each="spec : ${specAll}" th:value="${spec.id}">[[${spec.specName}]]</option>
                              </select>
                          </td>
                      </tr>

                      <tr>
                          <td>扩展属性</td>
                          <td>
                              <div class="btn-group">
                                  <button type="button" class="btn btn-default" title="新增扩展属性"  onclick="updateTable()"><i class="fa fa-file-o"></i> 新增扩展属性</button>

                              </div>
                              <table class="table table-bordered table-striped"  width="800px">
                                  <thead>
                                  <tr>
                                      <td><input type="checkbox" class="icheckbox_square-blue"></td>
                                      <td>属性名称</td>
                                      <td>操作</td>
                                  </tr>
                                  </thead>
                                  <tbody id="updateTable">
                                  </tbody>
                              </table>

                          </td>
                      </tr>

                  </table>

              </div>
              <div class="modal-footer">
                  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateTemplate()">保存</button>
                  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
              </div>
          </div>
      </div>
  </div>

</body>

</html>